package com.ldwx.center.service.impl;

import com.ldwx.center.dao.RecruitDao;
import com.ldwx.center.model.Company;
import com.ldwx.center.model.Job;
import com.ldwx.center.model.Recruit;
import com.ldwx.center.model.vo.RecruitCompanyVo;
import com.ldwx.center.service.CompanyService;
import com.ldwx.center.service.JobService;
import com.ldwx.center.service.RecruitService;
import com.ldwx.center.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.Date;
import java.util.List;

@Service
public class RecruitServiceImpl implements RecruitService{

    @Autowired
    private RecruitDao dao;
    @Autowired
    private CompanyService companyService;
    @Autowired
    private JobService jobService;

    @Override
    public List<RecruitCompanyVo> findAll() {
        return dao.getAll();
    }

    @Transactional
    @Override
    public Recruit save(Recruit recruit) {
        if(!StringUtils.isEmpty(recruit.getCompanyName())){
            //传了公司
            Company company = companyService.findCompanyByName(recruit.getCompanyName());
            if(company == null){
                //还未存在
                company = new Company();
                company.setCompanyName(recruit.getCompanyName());
                company.setCreateTime(new Date());
                company.setUpdateTime(new Date());
                companyService.addCompany(company);
            }
            //招聘表添加 公司id
            recruit.setCompanyId(company.getId());
        }

        if(!StringUtils.isEmpty(recruit.getJobName())){
            //传了岗位
            Job job = jobService.findJobByName(recruit.getJobName());
            if(job == null){
                job = new Job();
                job.setJobName(recruit.getJobName());
                job.setCreateTime(new Date());
                job.setUpdateTime(new Date());
                jobService.addJob(job);
            }
            recruit.setJobId(job.getId());
        }
        recruit.setCreateTime(new Date());
        recruit.setUpdateTime(new Date());
        return dao.save(recruit);
    }
}
